
using System;
using Gtk;

namespace finlasku
{
	
	
	[System.ComponentModel.ToolboxItem(true)]
	public partial class WNewBill : Gtk.Bin
	{

		private Gtk.ListStore billStore;
			
		public WNewBill()
		{
			this.Build();
			AppContext ctx = AppContextFactory.getContext();
			ctx.Rowupdated += RowUpdated;
		
			Gtk.CellRendererText billedunitRenderer = new Gtk.CellRendererText();
			billedunitRenderer.Editable = true;
			billedunitRenderer.Edited += billedunitEdited;
			billView.AppendColumn("Laskutettava erä", billedunitRenderer, "text", 0);
			
			Gtk.CellRendererText amountRenderer = new Gtk.CellRendererText();
			amountRenderer.Editable = true;
			amountRenderer.Edited += amountEdited;
			billView.AppendColumn("Määrä", amountRenderer, "text", 1);
			
			Gtk.CellRendererCombo unitRenderer = new Gtk.CellRendererCombo();
			unitRenderer.Editable = true;
			unitRenderer.Edited += unitEdited;
			Gtk.ListStore unitStore = new Gtk.ListStore(typeof(string));
			unitStore.AppendValues("kpl");
			unitStore.AppendValues("h");
			unitStore.AppendValues("yksikköä");
			unitRenderer.Model = unitStore;
			unitRenderer.TextColumn = 0;
			billView.AppendColumn("Yksikkö", unitRenderer, "text", 2);
			
			Gtk.CellRendererText unitpriceRenderer = new Gtk.CellRendererText();
			unitpriceRenderer.Editable = true;
			unitpriceRenderer.Edited += unitPriceEdited;
			billView.AppendColumn("Yksikköhinta", unitpriceRenderer, "text", 3);
			
			Gtk.CellRendererCombo alvRenderer = new Gtk.CellRendererCombo();
			alvRenderer.Editable = true;
			alvRenderer.Edited += vatEdited;
			Gtk.ListStore vatStore = new Gtk.ListStore(typeof(String));
			vatStore.AppendValues("0");
			vatStore.AppendValues("7");
			vatStore.AppendValues("12");
			vatStore.AppendValues("17");
			vatStore.AppendValues("22");
			alvRenderer.Model = vatStore;
			alvRenderer.TextColumn = 0;
			billView.AppendColumn("Alv %", alvRenderer, "text", 4);
			
			Gtk.CellRendererText noVatPriceRenderer = new Gtk.CellRendererText();
			noVatPriceRenderer.Editable = false;
			billView.AppendColumn("Veroton hinta", noVatPriceRenderer, "text", 5);				
			
			billStore = new Gtk.ListStore(typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string));
			
			//billStore.Append();
			
			billView.Model = billStore;
		}
		
		void RowUpdated(int rownumber) {
					
		}

		
		private void billedunitEdited(object o, Gtk.EditedArgs args)
		{
			Gtk.TreeIter iter;
			billStore.GetIter(out iter, new Gtk.TreePath(args.Path));
			
			billStore.SetValue(iter, 0, args.NewText);
		}
		
		private void amountEdited(object o, Gtk.EditedArgs args)
		{
			Gtk.TreeIter iter;
			billStore.GetIter(out iter, new Gtk.TreePath(args.Path));
			
			billStore.SetValue(iter, 1, args.NewText);
		}

		private void unitEdited(object o, Gtk.EditedArgs args)
		{
			Gtk.TreeIter iter;
			billStore.GetIter(out iter, new Gtk.TreePath(args.Path));
			
			billStore.SetValue(iter, 2, args.NewText);
		}
		
		private void unitPriceEdited(object o, Gtk.EditedArgs args)
		{
			Gtk.TreeIter iter;
			billStore.GetIter(out iter, new Gtk.TreePath(args.Path));
			
			billStore.SetValue(iter, 3, args.NewText);
		}
		
		private void vatEdited(object o, Gtk.EditedArgs args)
		{
			Gtk.TreeIter iter;
			billStore.GetIter(out iter, new Gtk.TreePath(args.Path));
			
			billStore.SetValue(iter, 4, args.NewText);
		}


		protected virtual void printClicked (object sender, System.EventArgs e)
		{
			Gtk.PrintOperationAction printAction = new PrintOperationAction();
			InvoicePrintOperation print = new InvoicePrintOperation();
			print.Run(printAction, null);
		}

		protected virtual void addRowClicked (object sender, System.EventArgs e)
		{
			billStore.Append();
		}

	}
}
